var listType = require('list.js');
var collectItemType = require('collectItem.js');
var util = require('../utils/util.js');
var editItemType = require('editItem.js');

var colorCal = require('color.js');
var itemcolorCal = colorCal.collectColor;
var app = getApp();
var store = app.store;
(function(){
    var collectList = listType.createList();
    collectList.initCollectList = function(page, winWidth, winHeight, renderParam, todoList) {
        this.initList(page, winWidth, winHeight, renderParam);
        
        this.pullDownCreateText = '下拉创建';
        this.releaseCreateText = '释放创建';

        this.itemcolorCal = itemcolorCal;
        
        this.setAnimationParam('collectListAnimation');
        this.todoList = todoList;
        this.topItem = editItemType.createEditItem();
        this.topItem.init(this.page, this, 'collectTopItem', 1, true);
        this.topItem.setTop(-app.ITEM_HEIGHT);
        this.topItem.setColor('RGB(23, 128, 247)');
        this.topItem.setHeight(app.ITEM_HEIGHT);
        this.topItem.setWidth(winWidth);
        this.topItem.setCountWH(app.ITEM_HEIGHT, app.ITEM_HEIGHT);
        this.bottomItem = editItemType.createEditItem();
        this.bottomItem.init(this.page, this, 'collectBottomItem', 1, false);
        this.bottomItem.setHeight(app.ITEM_HEIGHT);
        this.bottomItem.setWidth(winWidth);
        this.bottomItem.setCountWH(app.ITEM_HEIGHT, app.ITEM_HEIGHT);
        this.bottomItem.smoothRotateX(-90);
        this.topItem.hide();
        this.bottomItem.hide();
        this.renderPage();


        var s = this;
        this.setTouchcb(
            function(e, type, args) {

            },
            function(e, type, args) {
                s.dragmove(args);
            },
            function(e, type, args) {
                s.dragend(args);

            },
            function(e, type, args) {

            }
        );
    };

    collectList.load = function(dataList) {
        this.loadData(dataList);
        this.updatePosition();
        this.renderPage();
    };

    collectList.save = function() {
        app.store.saveCollectList(this.dataList);
    };

    collectList.createItem = function(data) {
        var item = collectItemType.createItem();
        item.initItem(this.page, this, data, this.winWidth, this.renderParam);
        item.handleCollectData();
        return item;
    };

    collectList.createNewData = function(order) {
        return app.store.createNewCollect('', order, 1, 0);
    };

    collectList.getCurrentCollectItem = function() {
        return this.getItemById(this.currentCollectId);
    };

    collectList.pullDownShow = function() {
        this.smoothMoveY(this.lastPos);
        wx.setNavigationBarTitle({title:'clear'});
    };

    collectList.positionForPullDown = function() {
        this.moveY(-this.height - app.CREATE_ITEM_THRESHOLD);
    };

    collectList.itemToRenderData = function(item, isMove) {
        var renderData = {};
        renderData.id = item.getId();
        renderData.text = item.getText();
        renderData.sliderColor = item.sliderColor;
        renderData.count = item.count;
        renderData.imgTop = item.imgTop;
        renderData.imgSize = item.imgSize;
        renderData.z_index = 1;
        renderData.countWidth = app.ITEM_HEIGHT;
        renderData.textColor = item.textColor;
        renderData.height = item.height;
        renderData.collectTextWidth = app.COLLECT_TEXT_WIDTH;
        renderData.opacity = renderData.opacity;
        renderData.display = item.display;
        if (isMove) {
            renderData.animation = item.getMoveYData();
        }
        return renderData;
    };

    collectList.open = function(openItem) {
        this.lastPost = this.y;
        this.currentCollectId = openItem.getId();
        var dataList = store.getItemListFromStoreSync(this.currentCollectId);

        if (!dataList) {
            dataList = [];
            store.saveItemList(this.currentCollectId, dataList);
        }
        this.todoList.reload(openItem, dataList);
        var sy = this.y + openItem.y;
        this.todoList.moveY(sy);
        console.log(sy);

        var at = openItem.getOrder();
        var i = this.items.length,
            item;
        var upDis = sy + openItem.height;
        var downDis = this.winHeight - (upDis);
        while (i--) {
            item = this.items[i];
            if (item.getOrder() <= at) {
                item.smoothMoveY(item.y - upDis);
            }
            else {
                item.smoothMoveY(item.y + downDis);
            }
        }
        this.todoList.openShow();
        this.page.currentList = this.todoList;
        app.setSetup(this.currentCollectId, util.SHOW_TYPE_ITEM);
    };

    collectList.doneItem = function(item) {
        if (item.getId() === this.todoList.getCollectId()) {
            this.todoList.doneAll();
        }
        this.renderPage();
        this.save();
    };

    collectList.delItem = function(item) {
        this.count--;
        if (item.getId() === this.todoList.getCollectId()) {
            this.todoList.del();
            this.currentCollectId = '';
            app.setSetup(this.currentCollectId, util.SHOW_TYPE_COLLECT);
        }
        var at = item.getOrder();
        this.dataList.splice(at, 1);
        this.deleteItemById(item.id);
        this.collapseAt(at, item);
        this.height -= item.height;
        this.updateBound(false);

        this.save();
    };

    collectList.onPullUp = function() {
        this.todoList.smoothMoveY(this.winHeight + app.CREATE_ITEM_THRESHOLD);
    };

    collectList.onLongPullUp = function() {
        if (this.todoList.isDel) {
            return;
        }
        this.todoList.pullUpShow();
        this.lastPos = this.upBound;
        this.smoothMoveY(-this.height - app.CREATE_ITEM_THRESHOLD);
        this.page.currentList = this.todoList;

        app.setSetup(this.currentCollectId, util.SHOW_TYPE_ITEM);
    };

    collectList.longPullUp = function() {
        if (!this._longPullUp) {
            this._longPullUp = true;
        }
        if (this.todoList.isDel) {
            return;
        }

        var topSwitch = this.todoList.topSwitch;
        topSwitch.show();
        var pos = this.y + Math.max(this.height + app.SWITCH_THRESHOLD, this.winHeight + app.CREATE_ITEM_THRESHOLD);
        this.todoList.moveY(pos);
        if (this.y < this.upBound - app.CREATE_ITEM_THRESHOLD) {
            if (!this._pastLongPullUpThreshold) {
               this._pastLongPullUpThreshold = true;
               topSwitch.downArrow();
           }
        }
        else {
            if (this._pastLongPullUpThreshold) {
                this._pastLongPullUpThreshold = false;
                topSwitch.upArrow();
            }
        };
    };

    collectList.unLongPullUp = function() {
        if (this._longPullUp) {
            this._longPullUp = false;
            this.todoList.moveY(this.winHeight + app.CREATE_ITEM_THRESHOLD);
            this.todoList.topSwitch.hide();
            if (this._pastLongPullUpThreshold) {
                this._pastLongPullUpThreshold = false;
                this.todoList.topSwitch.upArrow();
            }
        }
    };

    collectList.dragmove = function(args) {
        if (this.y > 0) {
            this.pullDown();
        }
        else {
            this.unPullDown();
        }
        if (this.y <= app.CREATE_ITEM_THRESHOLD) {
            this.unPastPullDownThreshold();
        }
        else {
            this.pastPullDownThreshold();
        }
        if (this.y <= this.upBound) {
            this.longPullUp();
        }
        else {
            this.unLongPullUp();
        }
        this.renderPage();
    };

    collectList.dragend = function(args) {
        this.resetMoveStates();
        if (this.y >= app.CREATE_ITEM_THRESHOLD) {
            this.onPullDown();
            args.isStop = true;
        }
        else if (this.y <= this.upBound - app.CREATE_ITEM_THRESHOLD) {
            if (this.todoList.isDel) {

            }
            else {
                this.onLongPullUp();
                args.isStop = true;
            }
          
        }
        else if (this.y <= this.upBound) {
           this.onPullUp();
        }
    };

    collectList.ontap = function(e) {
        if (this.movingTouch) {
            this.movingTouch = false;
            return;
        }
        if (this.isSorting) {
            this.isSorting = false;
            return;
        }
        console.log(e);
        var item = this.getItemById(e.target.id);
        if (item) {
            if (e.target.dataset.text === '1') {
                this.onEditStart(false, false, item);
            }
            else {
                this.open(item);
            }
            return;
        }
        this.onEditStart(true, false);
    };

    function createCollectList() {
        return Object.create(collectList);
    }

    module.exports = {
        createCollectList:createCollectList
    }
})();